﻿using System;
using System.Collections.Generic;
using System.Text;
using Castle.Core.Interceptor;
using System.Windows.Forms;


namespace DynamicProxyStudy
{
    public class MyInterceptor : IInterceptor //StandardInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            bool isSuccessfule = this.PreProceed(invocation);
            if (isSuccessfule == true)
            {
                this.PerformProceed(invocation);
            }

            this.PostProceed(invocation, isSuccessfule);
        }

        protected virtual void PerformProceed(IInvocation invocation)
        {
            invocation.Proceed();
        }

        protected virtual void PostProceed(IInvocation invocation, bool isValidePass)
        {
        }

        protected virtual bool PreProceed(IInvocation invocation)
        {
            bool isSuccessfule = true;
            /*权限验证等工作:验证通过后返回true；否则返回false*/

            object[] args = invocation.Arguments;
            if (args != null && args.Length > 0)
            {
                Button btn = args[0] as Button;
                if (btn != null)
                {
                    if (Program.DIC.Contains(btn.Name))
                    {
                        isSuccessfule = Convert.ToBoolean(Program.DIC[btn.Name]);
                    }

                    if (isSuccessfule == false)
                    {
                        MessageBox.Show("没有权限执行本功能,谢谢。");
                    }
                }
            }

            return isSuccessfule;
        }
    }
}
